<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$start		=	$_REQUEST['start'];
    $end		=	$_REQUEST['end'];
	
	switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("SELECT 	      sale_transaction_detail.id       AS id,
    	    									  sale_transaction.end_date        AS date,
                 					              production.name                  AS product,
    	    									  object.`name`                    AS obj_name,
                        						  SUM(sale_transaction_detail.product_quantity)  AS quantity,
												  IF(sale_transaction.pay_type = 1, 'ნაღდი', 'უნაღდო') AS `pay_type`,
				                                  ROUND(sale_transaction_detail.cost, 2)                 AS cost,
				                                  ROUND(SUM(sale_transaction_detail.product_quantity  *  sale_transaction_detail.cost), 2) AS sumCost
								 FROM         	  sale_transaction 
    	    									  LEFT JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id 
    	    		                              LEFT JOIN production ON sale_transaction_detail.production_id = production.id
    	    		                              LEFT JOIN object ON sale_transaction.object_id = object.id
								WHERE 			 sale_transaction.`status` = 0 AND DATE(sale_transaction.end_date) >= '$start' AND DATE(sale_transaction.end_date) <= '$end' AND sale_transaction.pay_type =1 AND sale_transaction_detail.id  IS NOT NULL
								GROUP BY      sale_transaction_detail.production_id, sale_transaction.object_id

UNION

								SELECT 	         sale_transaction_detail.id       AS id,
    	    									 sale_transaction.end_date        AS date,
                 					              production.name                  AS product,
    	    		                              object.`name`                    AS obj_name,
                        						  SUM(sale_transaction_detail.product_quantity)  AS quantity,
												  IF(sale_transaction.pay_type = 1, 'ნაღდი', 'უნაღდო') AS `pay_type`,
				                                  ROUND(sale_transaction_detail.cost, 2)                 AS cost,
				                                  ROUND(SUM(sale_transaction_detail.product_quantity  *  sale_transaction_detail.cost), 2) AS sumCost
								 FROM         	  sale_transaction 
    	    		                              LEFT JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id 
    	    		                              LEFT JOIN production ON sale_transaction_detail.production_id = production.id
    	    		                              LEFT JOIN object ON sale_transaction.object_id = object.id
								WHERE 			  sale_transaction.`status` = 0 AND DATE(sale_transaction.end_date) >= '$start' AND DATE(sale_transaction.end_date) <= '$end' AND sale_transaction.pay_type =2 AND sale_transaction_detail.id  IS NOT NULL
								GROUP BY      sale_transaction_detail.production_id, sale_transaction.object_id
    	    		
    	    		");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );

	        break;	    
	    default:
	       	echo "null";
	}
?>